
//创建cookie并存值
//document.cookie = "myName=Mr.Liu";
//document.cookie = "tel=1888888888";
//获取cookie值
// console.log(document.cookie);

//新增cookie
//addCookie();

//调用函数
//var result = getValueByKey('tel');
//console.log(result);

function addCookie(){
    var key = prompt("请输入key","");
    var value = prompt("请输入value","");
    //时间
    var time = new Date();
    time.setDate(time.getDate()+1);
    //封装添加cookie
    document.cookie=key+"="+value+"; expires="+time;
}

/**
 * 自定义方法
 * @param key 
 */
function getValueByKey(key){
    var arr = document.cookie.split("; ");
    for(var i=0; i<arr.length; i++){
        var k = arr[i].substring(0, arr[i].indexOf("="));
        var v = arr[i].substring(arr[i].indexOf("=")+1);
        if(key == k){
            return v;
        }
    }
    return "";
}

function check(){
    var zh = document.querySelector("[name='zh']").value;
    var mm = document.querySelector("[name='mm']").value;
    if(!zh || !mm){
        alert("不能为空！");
        return false;
    }

    //时间
    var time = new Date();
    time.setDate(time.getDate()+1);
    //封装添加cookie
    document.cookie="zh="+zh+"; expires="+time;
    document.cookie="mm="+mm+"; expires="+time;
    return true;
}

function init(){
    var arr = document.cookie.split("; ");
    if(arr.length > 0){
        var zh = getValueByKey("zh");
        var mm = getValueByKey("mm");
        document.getElementsByName("zh")[0].value=zh;
        document.getElementsByName("mm")[0].value=mm;
    }
}

function removeCookie(){
    document.cookie = "zh=; expires=";
}


function sessData(){
    sessionStorage.setItem("myAge","100");
}

function getSess(){
    var age = sessionStorage.getItem("myAge");
    alert(age);   
}

function localData(){
    localStorage.setItem("myTel","1888888888");
}

function getLocal(){
    var tel = localStorage.getItem("myTel");
    alert(tel);
}

function test(){
    var json1 = "{\"k\":\"v\"}";
    var json2 = "{\"id\":\"1001\",\"name\":\"zhangsan\"}";
    var json3 = "[{\"id\":\"1001\",\"name\":\"zhangsan\"},{\"id\":\"1002\",\"name\":\"lisi\"}]";

    //console.log(json1);
    //console.log(json2);
    console.log(json3);

    // var jsonObj = JSON.parse(json1);
    // var jsonObj = JSON.parse(json2);
    var jsonObjArr = JSON.parse(json3);

    //console.log(jsonObj.id,  jsonObj.name);
    for(var i=0; i<jsonObjArr.length; i++){
        console.log(jsonObjArr[i].id, jsonObjArr[i].name);
    }

    // var str = JSON.stringify(jsonObj);
    var str = JSON.stringify(jsonObjArr);
    console.log(str);
} 
//test();


//---------------------------------------------------
/*
//1、创建ajax请求对象
var req = new XMLHttpRequest();
//2、封装请求
//请求地址、请数据
var url = "../data/myData.txt?name=zhangsan";
//       提交方式,地址,异步true/同步false
req.open("get",url,true)
//3、设置回调函数
req.onreadystatechange=function(){
    if (req.readyState==4 && req.status==200){
       //获取响应结果：req.responseText
       //业务处理
    }
    else{
       //alert("请求失败"+req.status);
    }
}
//4、发送请求
req.send(null); // get方式发送请求
*/

function ajaxDemo(){
    //1、创建ajax请求对象
    var req = new XMLHttpRequest();

    //2、封装请求
    var url = "data/myData.json";
    req.open("get",url,true);
    //req.open("post",url,true);
    //post请求要设置头信息
    //req.setRequestHeader("Content-type","application/x-www-form-urlencoded");

    //3、设置回调函数
    req.onreadystatechange=function(){
        //readyState==4表示流程走到响应阶段。status==200表示正确返回结果
        if (req.readyState==4 && req.status==200){
            var result = req.responseText;
            console.log(result);
            result = JSON.parse(result);
            for(var i=0; i<result.length; i++){
                console.log(result[i].stuNo,result[i].stuName)
            }
        }
        // else{
        //     alert("请求失败"+req.status);
        // }
    }

    //4、发送请求
    //get方式发送请求[不带数据,数据在url里]
    req.send(null);
    //post方式发送请求[带数据]
    //req.send("{key:value}");

}


function getJsonServerData(){

    //1、创建ajax请求对象
    var req = new XMLHttpRequest();
    //2、封装请求
    var url = "http://localhost:3000/other";
    req.open("get",url,true);
    //3、设置回调函数
    req.onreadystatechange=function(){
        //readyState==4表示流程走到响应阶段。status==200表示正确返回结果
        if (req.readyState==4 && req.status==200){
            var result = req.responseText;
            console.log(result);
        }
    }
    //4、发送请求
    req.send(null);
}

//----------------------------------------------
//访问真实后端服务器tomcat

function getTomcatServerData(){
    //1、创建ajax请求对象
    var req = new XMLHttpRequest();
    //2、封装请求
    var url = "http://localhost:8888/Web30ServiceProject/EmployDataServlet";
    req.open("post",url,true);
    //3、设置回调函数
    req.onreadystatechange=function(){
        //readyState==4表示流程走到响应阶段。status==200表示正确返回结果
        if (req.readyState==4 && req.status==200){
            var result = req.responseText;
            console.log(result);
        }
    }
    //4、发送请求
    req.send(null);
}